let list = [1, 2, 3];
let index = null;
// 这是当前swiper-item在swiper中的索引
let currentIndex = 0;
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    course: {
      type: Array,
      value: ''
    },
    currentWeek: {
      type: Number,
      value: ''
    },
    startDate: {
      type: String,
      value: ''
    },
    sectionIndex: {
      type: Array,
      value: [4, 3, 1]
    }
  },
  observers: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    week: '',
    displayList: [],
    weekday: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
    weekIndex: new Date().getDay() == 0 ? 7 : new Date().getDay()
  },

  lifetimes: {
    attached: function () {
      this.upDateDisplayList();
      this.getTimeTable();
      this.setData({ startTime: Date.parse(this.data.startDate), wIndex: this.data.currentWeek });
      this.getWeekDate(this.data.startTime + (604800000 * (this.data.currentWeek - 1)));
    }
  },

  observers: {
    'course.*'() {
      this.getTimeTable();
    },
    'currentWeek'(e) {
      var week = `第${e}周`;;
      if (e <= 0 || e >= 21) week = '假期中';
      this.setData({ week });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 获取本周日期
    getWeekDate(startTime) {
      var date = new Date(startTime);
      var weekDate = [];
      var day = date.getDay();
      var firstDay = date.getDate() - day + 1;
      var lastDay = date.getDate() + (7 - day);
      for (var i = firstDay; i <= lastDay; i++) {
        var d = new Date(date.getFullYear(), date.getMonth(), i)
        var formatDate = this.formatDate(d);
        weekDate.push(formatDate);
      }
      this.setData({ weekDate });
    },
    //格局化日期：yyyy-MM-dd
    formatDate(date) {
      let m = date.getMonth() + 1;
      let d = date.getDate();
      if (m < 10) m = "0" + m;
      if (d < 10) d = "0" + d;
      return (m + "/" + d);
    },

    getTimeTable() {
      let morning = [];
      let afternoon = [];
      let night = [];
      let clen = 0;
      let list = this.properties.course;
      let grid = JSON.parse(JSON.stringify(this.properties.sectionIndex));
      grid[0] = grid[0] + 1;
      grid[1] = grid[1] + 1;
      grid[2] = grid[2] + 1;
      const color = [
        '.bg-yellow-thin',
        '.bg-orange-thin',
        '.bg-red-thin',
        '.bg-pink-thin',
        '.bg-mauve-thin',
        '.bg-purple-thin',
        '.bg-blue-thin',
        '.bg-cyan-thin',
        '.bg-green-thin',
        '.bg-olive-thin'
      ];
      for (let i = 0; i < list.length; i++) {
        for (let j = 0; j < list[i].weeks.length; j++) {
          list[i].bgcolor = color[Math.floor(Math.random() * color.length)];
          if (list[i].weeks[j] == this.data.currentWeek) {
            clen += (list[i].sections[1] - list[i].sections[0]) + 1;
            let am, pm, en = null;
            for (let s = list[i].sections[0]; s <= list[i].sections[1]; s++) {
              let data = JSON.parse(JSON.stringify(list[i]));
              if (s <= grid[0] && s < grid[0] + 1) {
                data.sections[1] = list[i].sections[1] > grid[0] ? grid[0] : list[i].sections[1];
                am = data;
              }
              if (s >= grid[0] + 1 && s < grid[0] + grid[1] + 1) {
                data.sections[0] = list[i].sections[0] < grid[0] + 1 ? grid[0] + 1 : list[i].sections[0];
                data.sections[1] = list[i].sections[1] > grid[0] + grid[1] ? grid[0] + grid[1] : list[i].sections[1];
                pm = data;
              }
              if (s > grid[0] + grid[1]) {
                data.sections[0] = list[i].sections[0] < grid[0] + grid[1] + 1 ? grid[0] + grid[1] + 1 : list[i].sections[0];
                en = data;
              }
            }
            if (am != null) {
              morning.push(am);
            }
            if (pm != null) {
              afternoon.push(pm);
            }
            if (en != null) {
              night.push(en);
            }
          }
        }
      }
      index = this.data.currentWeek;
      const sectionNum = grid.reduce((x, y) => x + y);
      let len = (sectionNum * 7) - clen;
      this.setData({
        sectionNum,
        morning,
        afternoon,
        night,
        len,
        grid
      });
    },

    // 更新当前displayList
    upDateDisplayList() {
      let displayList = [];
      displayList[currentIndex] = list[index];
      displayList[currentIndex - 1 == -1 ? 2 : currentIndex - 1] = list[index - 1 == -1 ? list.length - 1 : index - 1];
      displayList[currentIndex + 1 == 3 ? 0 : currentIndex + 1] = list[index + 1 == list.length ? 0 : index + 1];
      this.setData({ displayList });
    },

    onSwiperChange(e) {
      // 先判断是向前滑动还是向后滑动
      // current 为滑动后swiper-item的索引
      let current = e.detail.current
      // currentIndex是滑动前swiper-item的索引
      // 如果两者的差为2或者-1则是向后滑动
      if (currentIndex - current == 2 || currentIndex - current == -1) {
        index = index + 1 == list.length ? 0 : index + 1;
        currentIndex = currentIndex + 1 == 3 ? 0 : currentIndex + 1;
        this.upDateDisplayList();
        if (this.data.currentWeek >= 20) {
          return;
        }
        this.setData({ currentWeek: this.data.currentWeek + 1 });
        this.getWeekDate(this.data.startTime + (604800000 * (this.data.currentWeek - 1)));
        this.getTimeTable();
      }
      // 如果两者的差为-2或者1则是向前滑动
      else if (currentIndex - current == -2 || currentIndex - current == 1) {
        index = index - 1 == -1 ? list.length - 1 : index - 1;
        currentIndex = currentIndex - 1 == -1 ? 2 : currentIndex - 1;
        this.upDateDisplayList();
        if (this.data.currentWeek <= 0) {
          return;
        }
        this.setData({ currentWeek: this.data.currentWeek - 1 });
        this.getWeekDate(this.data.startTime + (604800000 * (this.data.currentWeek - 1)));
        this.getTimeTable();
      }
    }
  }
})